package com.jvm.getthreadresult;

import java.util.concurrent.*;

// ExecutorService.submit 方法实现
public class Demo3 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建一个线程池
        ExecutorService executorService = Executors.newCachedThreadPool();
        System.out.println(System.currentTimeMillis());
        Future<Integer> future = executorService.submit(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return 10;
        });
        // 关闭线程池
        executorService.shutdown();
        System.out.println(System.currentTimeMillis());
        Integer result = future.get();
        System.out.println(System.currentTimeMillis() + ":" + result);

        /**
         *
         * 使用ExecutorService.submit方法实现的，
         * 此方法返回一个Future，future.get()会让当前线程阻塞，
         * 直到 Future 关联的任务执行完毕。
         *
         */
    }

}
